# Clean up
rm(list = ls())
Sys.setenv(LANG = "en")

# Set working directory: please set your own
#setwd("~/Dropbox/sanctions_replication/")

# Load necessary packages
library(tidyverse)
library(lubridate)
library(readstata13)

# Import dataset
data <- read.dta13("data/survey_clean.dta")

# Make the plot
figureb7 <- data %>%
  select(
    sanct_phis_vox_num, sanct_verb_vox_num, sanct_insult_vox_num, 
    sanct_avoid_vox_num, sanct_deny_vox_num, sanct_gossip_vox_num, no_sanct_vox_num,
    exp_sanct_phis_vox_num, exp_sanct_verb_vox_num, exp_sanct_insult_vox_num, 
    exp_sanct_avoid_vox_num, exp_sanct_deny_vox_num, exp_sanct_gossip_vox_num, exp_no_sanct_vox_num,
    sanct_phis_psoe_num, sanct_verb_psoe_num, sanct_insult_psoe_num, 
    sanct_avoid_psoe_num, sanct_deny_psoe_num, sanct_gossip_psoe_num, no_sanct_psoe_num,
    exp_sanct_phis_psoe_num, exp_sanct_verb_psoe_num, exp_sanct_insult_psoe_num, 
    exp_sanct_avoid_psoe_num, exp_sanct_deny_psoe_num, exp_sanct_gossip_psoe_num, exp_no_sanct_psoe_num,
    sanct_phis_up_num, sanct_verb_up_num, sanct_insult_up_num, 
    sanct_avoid_up_num, sanct_deny_up_num, sanct_gossip_up_num, no_sanct_up_num,
    exp_sanct_phis_up_num, exp_sanct_verb_up_num, exp_sanct_insult_up_num, 
    exp_sanct_avoid_up_num, exp_sanct_deny_up_num, exp_sanct_gossip_up_num, exp_no_sanct_up_num
  ) %>%
  `colnames<-`(c("Vox_Physical", "Vox_Verbal", "Vox_Insult",
                 "Vox_Avoid", "Vox_Deny", "Vox_Gossip", "Vox_Nothing",
                 "Vox_Physical_Others", "Vox_Verbal_Others", "Vox_Insult_Others",
                 "Vox_Avoid_Others", "Vox_Deny_Others", "Vox_Gossip_Others", "Vox_Nothing_Others",
                 "PSOE_Physical", "PSOE_Verbal", "PSOE_Insult",
                 "PSOE_Avoid", "PSOE_Deny", "PSOE_Gossip", "PSOE_Nothing",
                 "PSOE_Physical_Others", "PSOE_Verbal_Others", "PSOE_Insult_Others",
                 "PSOE_Avoid_Others", "PSOE_Deny_Others", "PSOE_Gossip_Others", "PSOE_Nothing_Others",
                 "UP_Physical", "UP_Verbal", "UP_Insult",
                 "UP_Avoid", "UP_Deny", "UP_Gossip", "UP_Nothing",
                 "UP_Physical_Others", "UP_Verbal_Others", "UP_Insult_Others",
                 "UP_Avoid_Others", "UP_Deny_Others", "UP_Gossip_Others", "UP_Nothing_Others"
  )) %>%
  pivot_longer(cols = c(Vox_Physical:UP_Nothing_Others),
               names_to = "Group") %>%
  group_by(Group) %>%
  mutate(n = sum(!is.na(value))) %>%
  mutate(mean = mean(value, na.rm = T)) %>%
  mutate(sd = sd(value, na.rm = T)) %>%
  mutate(std_error = sd/sqrt(n)) %>%
  mutate(ci_upper = mean + 1.96 * std_error) %>%
  mutate(ci_lower = mean - 1.96 * std_error) %>%
  ungroup() %>%
  mutate(Outcome = ifelse(Group %in% c("Vox_Physical", "Vox_Verbal", "Vox_Insult",
                                       "Vox_Avoid", "Vox_Deny", "Vox_Gossip", "Vox_Nothing",
                                       "PSOE_Physical", "PSOE_Verbal", "PSOE_Insult",
                                       "PSOE_Avoid", "PSOE_Deny", "PSOE_Gossip", "PSOE_Nothing",
                                       "UP_Physical", "UP_Verbal", "UP_Insult",
                                       "UP_Avoid", "UP_Deny", "UP_Gossip", "UP_Nothing"), "Own views on appropriateness", "Perceptions of others' views on appropriateness")) %>%
  mutate(Nothing = ifelse(Group %in% c("Vox_Nothing", "Vox_Nothing_Others",
                                       "UP_Nothing", "UP_Nothing_Others",
                                       "PSOE_Nothing", "PSOE_Nothing_Others"), 1, 0)) %>%
  mutate(Party = ifelse(Group %in% c("Vox_Physical", "Vox_Verbal", "Vox_Insult",
                                     "Vox_Avoid", "Vox_Deny", "Vox_Gossip", "Vox_Nothing",
                                     "Vox_Physical_Others", "Vox_Verbal_Others", "Vox_Insult_Others",
                                     "Vox_Avoid_Others", "Vox_Deny_Others", "Vox_Gossip_Others", "Vox_Nothing_Others"), "Vox", 
                        ifelse(Group %in% c("UP_Physical", "UP_Verbal", "UP_Insult",
                                            "UP_Avoid", "UP_Deny", "UP_Gossip", "UP_Nothing",
                                            "UP_Physical_Others", "UP_Verbal_Others", "UP_Insult_Others",
                                            "UP_Avoid_Others", "UP_Deny_Others", "UP_Gossip_Others", "UP_Nothing_Others"), "Podemos",
                               ifelse(Group %in% c("PSOE_Physical", "PSOE_Verbal", "PSOE_Insult",
                                                   "PSOE_Avoid", "PSOE_Deny", "PSOE_Gossip", "PSOE_Nothing",
                                                   "PSOE_Physical_Others", "PSOE_Verbal_Others", "PSOE_Insult_Others",
                                                   "PSOE_Avoid_Others", "PSOE_Deny_Others", "PSOE_Gossip_Others", "PSOE_Nothing_Others"), "PSOE", NA)))) %>%
  mutate(Sanction = ifelse(Group %in% c("Vox_Physical", "UP_Physical", "PSOE_Physical", "Vox_Physical_Others", "UP_Physical_Others", "PSOE_Physical_Others"), "Physical",
                           ifelse(Group %in% c("Vox_Verbal", "UP_Verbal", "PSOE_Verbal", "Vox_Verbal_Others", "UP_Verbal_Others", "PSOE_Verbal_Others"), "Verbal",
                                  ifelse(Group %in% c("Vox_Insult", "UP_Insult", "PSOE_Insult", "Vox_Insult_Others", "UP_Insult_Others", "PSOE_Insult_Others"), "Insult",
                                         ifelse(Group %in% c("Vox_Avoid", "UP_Avoid", "PSOE_Avoid", "Vox_Avoid_Others", "UP_Avoid_Others", "PSOE_Avoid_Others"), "Avoid\ninteraction",
                                                ifelse(Group %in% c("Vox_Deny", "UP_Deny", "PSOE_Deny", "Vox_Deny_Others", "UP_Deny_Others", "PSOE_Deny_Others"), "Deny help",
                                                       ifelse(Group %in% c("Vox_Gossip", "UP_Gossip", "PSOE_Gossip", "Vox_Gossip_Others", "UP_Gossip_Others", "PSOE_Gossip_Others"), "Gossip",
                                                              ifelse(Group %in% c("Vox_Nothing", "UP_Nothing", "PSOE_Nothing", "Vox_Nothing_Others", "UP_Nothing_Others", "PSOE_Nothing_Others"), "Do nothing",
                                                                     NA)))))))) %>%
  mutate(Outcome = fct_reorder(Outcome, Nothing, .desc = TRUE)) %>%
  ggplot(aes(x = Sanction, y = value, color = Party)) +
  scale_color_manual(values = c("purple", "red", "green4")) +
  theme_bw() +
  geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = 0.1, position = position_dodge(width = 0.5)) + 
  geom_point(aes(y = mean), position = position_dodge(width = 0.5)) +
  # theme(legend.position = "none") +
  facet_wrap(. ~ Outcome) +
  labs(x = "", y = "") +
  scale_y_continuous(breaks = c(1, 2, 3, 4, 5, 6))
figureb7

ggsave("figures/figureb7.png", plot = figureb7, width = 27.5, height = 15, units = "cm")